package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.entity.Subjects;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.service.ISubjectsService;
import com.xmy.cultivate.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 科目 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/api/subjects")
public class SubjectsController extends BaseController{
    @Autowired
    ISubjectsService iSubjectsService;

    @Autowired
    IdWorker idWorker;

    /**
     *
     * @param pageNum
     * @param pageSize
     * @param type 0：全部，1：只获取type为1的科目，2：获取的是type为1,2的科目
     * @param other 1：添加其它选项
     * @return
     */
    @GetMapping()
    public Result findAll(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                          @RequestParam(value = "type",required = false,defaultValue = "0") Integer type,
                          @RequestParam(value = "other",required = false,defaultValue = "0") Integer other
                          ){
        try{
            IPage<Subjects> iPage = new Page<>(pageNum,pageSize);
            QueryWrapper<Subjects> queryWrapper = new QueryWrapper<>();
            //queryWrapper.ne("id","4");//暂时不显示语文
            if (!type.equals(0)){
                queryWrapper.eq("type",type);
            }
            iSubjectsService.page(iPage,queryWrapper);
            if (other.equals(1)){
                if (!iPage.getRecords().isEmpty()){
                    List<Subjects> list = iPage.getRecords();
                    Subjects subject = new Subjects();
                    subject.setId(0L);
                    subject.setName("其它");
                    subject.setStatus(1);
                    list.add(subject);
                    iPage.setRecords(list);
                }
            }


            return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id){
        try{
            Subjects subjects = iSubjectsService.getById(id);
            //long ids =  idWorker.nextId();
            //System.out.println(ids);
            return new Result(Code.OK,subjects);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }


    @PostMapping()
    public Result save(@RequestBody Subjects subjects){
        try{
            //subjects.setId(idWorker.nextId());
            //subjects.setCreatedAt(DateUtil.getDateTime());
            boolean rs = iSubjectsService.save(subjects);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id,@RequestBody Subjects subjects){
        try{
            //subjects.setUpdatedAt(DateUtil.getDateTime());
            boolean rs = iSubjectsService.updateById(subjects);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        try{
            boolean rs =  iSubjectsService.removeById(id);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }
}
